package com.sanhuo.idms.utils;


import com.sanhuo.MapperProxy;
import com.sanhuo.idms.dao.ClasMapper;
import com.sanhuo.idms.dao.CollegeMapper;
import com.sanhuo.idms.dao.DormMapper;
import com.sanhuo.idms.dao.TungMapper;
import com.sanhuo.idms.entity.Clas;
import com.sanhuo.idms.entity.College;
import com.sanhuo.idms.entity.Dorm;
import com.sanhuo.idms.entity.Tung;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;


public class SanhuoUtils {
    private final String beginDate = "1995-01-01";
    private final String endDate = "2000-02-03";
    private Random random = new Random();
    private final GetString getS = new GetString();
    private CollegeMapper collegeMapper = MapperProxy.ProxyFactory(CollegeMapper.class);
    private TungMapper tungMapper = MapperProxy.ProxyFactory(TungMapper.class);
    private DormMapper dormMapper = MapperProxy.ProxyFactory(DormMapper.class);
    public ClasMapper clasMapper = MapperProxy.ProxyFactory(ClasMapper.class);

    String FIST_NAME[] = getS.FIST_NAME;

    public String getTeacherId() {
        return teacherId;
    }

    String ENGLISH[] = getS.ENGLISH;
    String EMAIL[] = getS.EMAIL;
    String LAST_NAME[] = getS.LAST_NAME;
    String GENDER[] = getS.GENDER;
    String COLLEGE[] = getS.COLLEGE;
    private StringBuffer name = new StringBuffer();
    private Integer age;
    private String birthday;
    private Integer grade;
    private String teacherId;
    private String studentId;
    private String gender;
    private String college;
    private String password;
    private String tung;
    private String clas;
    private String drom;
    private String qq;
    private String email;
    private String phone;

    {
        StringBuilder qqsb = new StringBuilder();
        for (int i = 0; i < random.nextInt(8) + 2; i++) {
            qqsb.append(Integer.toString(random.nextInt(9) + 1));
        }
        qq = qqsb.toString();

    }

    public String getTung() {
        return tung;
    }

    public void setTung(String tung) {
        this.tung = tung;
    }

    {
        List<Tung> tungs = tungMapper.findAll();
        this.tung = tungs.get(random.nextInt(tungs.size() - 1) + 1).getId();

    }

    //初始化email
    {
        StringBuffer resultmail = new StringBuffer();
        for (int i = 0; i < random.nextInt(5) + 8; i++) {
            resultmail.append(ENGLISH[random.nextInt(ENGLISH.length - 1)]);
        }
        resultmail.append(EMAIL[random.nextInt(EMAIL.length - 1)]);
        this.email = resultmail.toString();

    }

    //初始化phone
    {
        StringBuffer resultphone = new StringBuffer();
        resultphone.append("13");
        for (int i = 0; i < 8; i++) {
            resultphone.append(Integer.toString(random.nextInt(9) + 1));
        }
        this.phone = resultphone.toString();
    }

    public String getEmail() {
        return email;
    }

    public String getPhone() {
        return phone;
    }

    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    //初始college;
    {
        List<College> colleges = collegeMapper.findAll();
        this.college = colleges.get(random.nextInt(colleges.size() - 1) + 1).getId();
    }


    //初始化grade
    {
        Integer[] grades = {15, 16, 17, 18};
        this.grade = grades[random.nextInt(4) ];

    }


    //初始化name
    {

        this.name.append(FIST_NAME[random.nextInt(FIST_NAME.length - 1)])
                .append(LAST_NAME[random.nextInt(LAST_NAME.length - 1)])
                .append(LAST_NAME[random.nextInt(LAST_NAME.length - 1)]);

    }

    //初始化gender
    {
        this.gender = GENDER[random.nextInt(2)];
    }

    //初始化birthday
    {

        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = (Date) format.parse(beginDate);// 构造开始日期
            Date end = (Date) format.parse(endDate);// 构造结束日期
            // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。

            long date = randomTime(start.getTime(), end.getTime());
            DateFormat df1 = DateFormat.getDateInstance();
            this.birthday = df1.format(new Date(date));

        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    //初始化age
    {
        DateFormat df1 = DateFormat.getDateInstance();
        String td = df1.format(new Date()).split("-")[0];
        String bd = this.birthday.split("-")[0];
        this.age = Integer.parseInt(td) - Integer.parseInt(bd);

    }


    private static long randomTime(long begin, long end) {
        long rtn = begin + (long) (Math.random() * (end - begin));
        // 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值
        if (rtn == begin || rtn == end) {
            return randomTime(begin, end);
        }
        return rtn;
    }

    //初始化tid
    {
        StringBuffer result1 = new StringBuffer();

        result1.append("teacher");
        result1.append(random.nextInt(9999));


        this.teacherId = result1.toString();
    }

    //初始化sid
    {
        StringBuffer result = new StringBuffer();
        if (this.gender == "男") {
            result.append("311");
        } else {
            result.append("321");
        }
        switch (this.grade) {
            case 18:
                result.append("800");
                break;
            case 17:
                result.append("700");
                break;
            case 16:
                result.append("600");
                break;
            case 15:
                result.append("500");
                break;
        }


        for (int i = 0; i < 4; i++) {
            result.append(Integer.toString(random.nextInt(9)));
        }

        this.studentId = result.toString();

    }


    {
        List<Dorm> dorms = dormMapper.findAll();
        this.drom = dorms.get(random.nextInt(dorms.size())).getId();
    }

    public String getDrom() {
        return drom;
    }

    public void setDrom(String drom) {
        this.drom = drom;
    }

    {
        List<Clas> clas = clasMapper.findAll(this.grade.toString());
        this.clas = clas.get(random.nextInt(clas.size())).getId();
    }

    public String getClas() {
        return clas;
    }

    public void setClas(String clas) {
        this.clas = clas;
    }

    public String getCollege() {
        return college;
    }

    public String getName() {
        return name.toString();
    }

    public Integer getAge() {
        return age;
    }

    public String getBirthday() {
        return birthday;
    }

    public Integer getGrade() {
        return grade;
    }

    public String getStudentId() {
        return studentId;
    }

    public String getGender() {
        return gender;
    }


}
